﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Yugi.Client.Card;
using System.Threading;

namespace Yugi.Client.Control
{
    public partial class YTableControl : UserControl, IInvoke
    {
        #region MF1


        /// <summary>
        /// Doi Trang Thai Handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void i_Click(object sender, RoutedEventArgs e)
        {
            MenuItem item = sender as MenuItem;
            YCard current = item.DataContext as YCard;
            CardModel cmodel = current.DataContext as CardModel;
            if (cmodel.DaSetViTri == false)
            {
                switch (current.State)
                {
                    case YTrangThaiLaBai.Attact_Up:
                        model.YSupervisorModel.ReceiveMessageFromChildren(ClientMSG.DoiCongThu, cmodel.YCard, Common.YMSG.CARD_TRANGTHAI_DUNG_MO);
                        cmodel.DaSetViTri = true;
                        current.State = YTrangThaiLaBai.Defend_Up;
                        break;
                    case YTrangThaiLaBai.Defend_Down:
                        model.YSupervisorModel.ReceiveMessageFromChildren(ClientMSG.DoiCongThu, cmodel.YCard, Common.YMSG.CARD_TRANGTHAI_NAM_UP);
                        current.State = YTrangThaiLaBai.Attact_Up;
                        cmodel.DaSetViTri = true;
                        break;
                    case YTrangThaiLaBai.Defend_Up:
                        model.YSupervisorModel.ReceiveMessageFromChildren(ClientMSG.DoiCongThu, cmodel.YCard, Common.YMSG.CARD_TRANGTHAI_NAM_MO);
                        current.State = YTrangThaiLaBai.Attact_Up;
                        cmodel.DaSetViTri = true;
                        break;
                }
            }
        }

        /// <summary>
        /// Sinh Menu doi Trang Thai Bai (MF)
        /// </summary>
        void InitMenuDoiCongThu()
        {
            foreach (var host_item in Data.Values)
            {
                
                YCard item = host_item.CardControl;
                // Bo wa bai doi thu
                if (item.IsBaiDoiThu) continue;
                int id=((CardModel)item.DataContext).YCard.ID;
                if (!Block.Block.Allow_ChangePosition.Contains(id)) continue;
                ((CardModel)item.DataContext).DaSetViTri = false;
                item.ContextMenu.Items.Clear();
                MenuItem i = new MenuItem();
                i.DataContext = item;
                i.Header = "Doi Trang Thai";
                i.Click += new RoutedEventHandler(i_Click);
                item.ContextMenu.Items.Add(i);
            }
        }

        /// <summary>
        /// Xoa Menu Doi Trang Thai Bai (MF)
        /// </summary>
        void DelMenuDoiCongThu()
        {
            foreach (var item in Data.Values)
            {
                item.CardControl.ContextMenu.Items.Clear();
            }
        }

        /// <summary>
        /// Hien Bai doi thu len
        /// </summary>
        /// <param name="card"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        void LenBaiDoiThu(YBaseCard card, int x, int y, Common.YMSG state)
        {
            YCard view;
            switch (state)
            {
                case Common.YMSG.CARD_TRANGTHAI_DUNG_UP:
                    view = new YCard(YTrangThaiLaBai.Attact_Down);
                    view.IsDown = true;
                    view.IsViewDetail = false;
                    break;
                case Common.YMSG.CARD_TRANGTHAI_NAM_UP:
                    view = new YCard(YTrangThaiLaBai.Defend_Down);
                    view.IsDown = true;
                    view.IsViewDetail = false;
                    break;
                case Common.YMSG.CARD_TRANGTHAI_DUNG_MO:
                    view = new YCard(YTrangThaiLaBai.Attact_Up);
                    view.IsViewDetail = true;
                    break;
                default:
                    view = new YCard();
                    break;
            }
            HostItem host = new HostItem(view, Yugi_Gui.Linker.MainWindow.ActualWidth/2,0);
            Data[card.ID] = host;
            view.IsBaiDoiThu = true;
            view.DataContext = new CardModel(card, model.YSupervisorModel);
            model.YSupervisorModel.Doi_Thu.YBaiTrenBan.Add((CardModel)view.DataContext);

            this.container.Children.Add(host.Host);

            Grid.SetRow(host.Host, 4 - x);
            Grid.SetColumn(host.Host, 6 - y);
            ViTri[4 - x, 6 - y] = true;
           
        }



        /// <summary>
        /// The Hien Doi Cong Thu Nguoi Choi COn Lai
        /// </summary>
        /// <param name="card"></param>
        /// <param name="msg"></param>
        void DoiCongThuDoiThu(int card_id, Common.YMSG msg)
        {
            YCard item = Data[card_id].CardControl;
            switch (msg)
            {
                case Common.YMSG.CARD_TRANGTHAI_DUNG_MO:
                    item.State = YTrangThaiLaBai.Attact_Up;
                    item.IsViewDetail = true;
                    break;
                case Common.YMSG.CARD_TRANGTHAI_NAM_MO:
                    item.State = YTrangThaiLaBai.Defend_Up;
                    item.IsViewDetail = true;
                    break;
                case Common.YMSG.CARD_TRANGTHAI_DUNG_UP:
                    item.State = YTrangThaiLaBai.Attact_Down;
                    item.IsViewDetail = true;
                    break;
            }
        }

        /// <summary>
        /// Vi tri tri theo co the dat dc bai magic
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        void FindNextPosMagic(out int x, out int y)
        {
            x = 4;
            y = 1;
            //foreach (var item in model.YSupervisorModel.Nguoi_Choi.YBaiTrenBan)
            //{
            //    if (item.YCard is YSpellCard || item.YCard is YTrapCard)
            //        y++;
            //}
            for (int i = y; i <= 5; i++)
            {
                if (this.ViTri[x, i] == false)
                {
                    y = i;
                    this.ViTri[x, i] = true;
                    return;
                }
            }
        }
        /// <summary>
        /// Vi tri tri theo co the dat dc bai magic
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        void FindNextPosNormal(out int x, out int y)
        {
            x = 3;
            y = 1;
            for (int i = y; i <= 5; i++)
            {
                if (this.ViTri[x, i] == false)
                {
                    y = i;
                    this.ViTri[x, i] = true;
                    return;
                }
            }
        }
        void LenBaiMagic(YBaseCard card,Common.YMSG state)
        {
            int x, y;
            
            FindNextPosMagic(out x, out y);
            if (card is YSpellCard || card is YTrapCard)
            {
                YCard view = new YCard(YTrangThaiLaBai.Attact_Up);
                HostItem host = new HostItem(view, Yugi_Gui.Linker.MainWindow.ActualWidth / 2, Yugi_Gui.Linker.MainWindow.ActualHeight);
                view.DataContext = new CardModel(card, model.YSupervisorModel);
                // Set Khong Cho Phep Doi Cong Thu Tren L Bai Moi dc Summon
                ((CardModel)view.DataContext).DaSetViTri = true;
                // Them vo View
                this.container.Children.Add(host.Host);
                Grid.SetRow(host.Host, x);
                Grid.SetColumn(host.Host, y);

                // Gan Bai Vao Database luu tru
                Data[card.ID] = host;
                // Send Bao Cao Len Server Card X Y Trang Thai
                if(card.CardType== YType.Spell)
                    model.YSupervisorModel.ReceiveMessageFromChildren(ClientMSG.LenBaiSpellXong, card, x, y, Common.YMSG.CARD_TRANGTHAI_DUNG_UP);
                else if(card.CardType== YType.Trap)
                    model.YSupervisorModel.ReceiveMessageFromChildren(ClientMSG.LenBaiTrapXong, card, x, y, Common.YMSG.CARD_TRANGTHAI_DUNG_UP);
                // Them Bai vao List Bai Nguoi Choi
                model.YSupervisorModel.Nguoi_Choi.YBaiTrenBan.Add((CardModel)view.DataContext);
                if(state== Common.YMSG.CARD_TRANGTHAI_DUNG_UP)
                    view.State = YTrangThaiLaBai.Attact_Down;
                else
                    view.State = YTrangThaiLaBai.Attact_Up;
            }
        }

        void LenBaiNormal()
        {
            if (queue.Count == 0)
            {

                return;
            }
            int x, y;
            FindNextPosNormal(out x, out y);
            YBaseCard temp = queue.Pop();
            Common.YMSG trang_thai = state.Pop();

            if (!(temp is YMonsterCard)) 
                return;

            YCard view = new YCard();
            HostItem host = new HostItem(view, Yugi_Gui.Linker.MainWindow.ActualWidth/2,Yugi_Gui.Linker.MainWindow.ActualHeight);
            view.DataContext = new CardModel(temp, model.YSupervisorModel);
            // Set Khong Cho Phep Doi Cong Thu Tren L Bai Moi dc Summon
            ((CardModel)view.DataContext).DaSetViTri = true;
            // Them vo View
            this.container.Children.Add(host.Host);
        
            Grid.SetRow(host.Host, x);
            Grid.SetColumn(host.Host, y);


            // Gan Bai Vao Database luu tru
            Data[temp.ID] = host;

            // Send Bao Cao Len Server Card X Y Trang Thai
            model.YSupervisorModel.ReceiveMessageFromChildren(ClientMSG.LenBaiXong, temp, x, y, trang_thai);
            // Them Bai vao List Bai Doi Thu
            model.YSupervisorModel.Nguoi_Choi.YBaiTrenBan.Add((CardModel)view.DataContext);

            
            switch (trang_thai)
            {
                case Common.YMSG.CARD_TRANGTHAI_DUNG_MO:
                    view.State = YTrangThaiLaBai.Attact_Up;
                    break;
                case Common.YMSG.CARD_TRANGTHAI_NAM_UP:
                    view.State = YTrangThaiLaBai.Attact_Up;
                    view.State = YTrangThaiLaBai.Defend_Down;
                    break;
            }
           
        }
        //--------------------------------Special Summon-----------------------------//
        CardChoice choice;
        void LenBaiDacBiet(int number)
        {
            if(choice==null)
                choice = new CardChoice();
            choice.Count = number;
            choice.Data = this.Data;
            choice.ListCards = model.YSupervisorModel.Nguoi_Choi.YBaiTrenBan;
            choice.SelectionType = YType.Monster;
            choice.OnChoiceComplete += new EventHandler(choice_OnChoiceComplete);
            choice.OnError += new Action<object, string>(choice_OnError);
            choice.Begin();
        }

        void choice_OnError(object arg1, string arg2)
        {
            choice = arg1 as CardChoice;
            MessageBox.Show(arg2);

            //Del delegate 
            choice.OnChoiceComplete -= new EventHandler(choice_OnChoiceComplete);
            choice.OnError -= new Action<object, string>(choice_OnError);
        }

       
        void choice_OnChoiceComplete(object sender, EventArgs e)
        {
            CardChoice choice = sender as CardChoice;
            YBaseCard card = queue.Peek();
            Common.YMSG st = state.Peek();

            int x=3, y = 9999;

            int temp;
            for (int i = 0; i < choice.Choices.Count; i++)
            {
                GetPosition(out x, out temp, choice.Choices[i].ID);
                if (temp < y)
                    y = temp;
            }           


            //init params list
            List<object> param = new List<object>();
            param.Add(card.ID);
            param.Add(choice.Count);
            for (int i = 0; i < choice.Count; i++)
            {
                param.Add(choice.Choices[i].ID);
            }
            param.Add(x);
            param.Add(y);
            param.Add(st);

            // send
            model.YSupervisorModel.SendMessageToServer(Common.YMSG.CARD_LENBAI_SUMMONDACBIET, param.ToArray());

            // Del Delegate
            choice.OnChoiceComplete -= new EventHandler(choice_OnChoiceComplete);
            choice.OnError -= new Action<object, string>(choice_OnError);
            list_card_thay_the = choice.Choices;
            
        }

        void TheHienLenBaiDacBiet()
        {
            YBaseCard temp = queue.Pop();
            Common.YMSG trang_thai = state.Pop();
            foreach (var item in list_card_thay_the)
            {
                XoaBai(item.ID);
            }
            //Delay(500);
            int x, y;
            FindNextPosNormal(out x, out y);
            if (!(temp is YMonsterCard))
                return;

            YCard view = new YCard();
            HostItem host = new HostItem(view, Yugi_Gui.Linker.MainWindow.ActualWidth/2,Yugi_Gui.Linker.MainWindow.ActualHeight);
            view.DataContext = new CardModel(temp, model.YSupervisorModel);
            // Set Khong Cho Phep Doi Cong Thu Tren L Bai Moi dc Summon
            ((CardModel)view.DataContext).DaSetViTri = true;
            // Them vo View
            this.container.Children.Add(host.Host);

            Grid.SetRow(host.Host, x);
            Grid.SetColumn(host.Host, y);


            // Gan Bai Vao Database luu tru
            Data[temp.ID] = host;

            
            // Them Bai vao List Bai Nguoi Choi
            model.YSupervisorModel.Nguoi_Choi.YBaiTrenBan.Add((CardModel)view.DataContext);


            switch (trang_thai)
            {
                case Common.YMSG.CARD_TRANGTHAI_DUNG_MO:
                    view.State = YTrangThaiLaBai.Attact_Up;
                    break;
                case Common.YMSG.CARD_TRANGTHAI_NAM_UP:
                    view.State = YTrangThaiLaBai.Attact_Up;
                    view.State = YTrangThaiLaBai.Defend_Down;
                    break;
            }
        }

        void LenBaiDacBietDoiThu(YBaseCard temp,List<int> ids,int x,int y,Common.YMSG state)
        {
            foreach (var item in ids)
            {
                KiemTraLatBai(item);
                XoaBai(item);    
            }
            
            
           
            if (!(temp is YMonsterCard))
                return;

            YCard view = new YCard();
            HostItem host = new HostItem(view, Yugi_Gui.Linker.MainWindow.ActualWidth/2,0);
            view.DataContext = new CardModel(temp, model.YSupervisorModel);
            // Set Khong Cho Phep Doi Cong Thu Tren L Bai Moi dc Summon
            ((CardModel)view.DataContext).DaSetViTri = true;
            // Them vo View
            this.container.Children.Add(host.Host);

            Grid.SetRow(host.Host, x);
            Grid.SetColumn(host.Host, y);


            // Gan Bai Vao Database luu tru
            Data[temp.ID] = host;

           
            // Them Bai vao List Bai Doi Thu
            model.YSupervisorModel.Doi_Thu.YBaiTrenBan.Add((CardModel)view.DataContext);


            switch (state)
            {
                case Common.YMSG.CARD_TRANGTHAI_DUNG_MO:
                    view.State = YTrangThaiLaBai.Attact_Up;
                    break;
                case Common.YMSG.CARD_TRANGTHAI_NAM_UP:
                    view.State = YTrangThaiLaBai.Attact_Up;
                    view.State = YTrangThaiLaBai.Defend_Down;
                    break;
            }
        }
        #endregion
        List<YBaseCard> list_card_thay_the;
    }
}
